import requests
from bs4 import BeautifulSoup


def getHtml(url):
    try:
        re = requests.get(url, timeout=10)
        re.raise_for_status()
        re.encoding = 'utf-8'
        return re.text
    except:
        return ''


def getHtmlData(html):
    soup = BeautifulSoup(html, 'html.parser')
    div_con=soup.find_all('div',{'class':'list_con'})
    li=div_con[0].find_all('li')
    all_ls = []
    ls=[]
    for a in li:
        if len(li)!=1:
            ls.append(a.find_all('span').string)
            ls.append(a.find_all('a').string)
            all_ls.append(ls)
            ls=[]
    return all_ls


    # for ul in tas.find_all('ul'):
    #     ls = []
    #     for span in ul.find_all('span'):
    #         ls.append(span.string)
    #     for a in ul.find_all('a'):
    #         ls.append(a.string)
    #     all_ls.append(ls)
    # return all_ls


def write(ls):
    with open(r'新闻.csv', 'w') as f:
        for line in ls:
            f.write(','.join(line[0:4]) + '\n')


def main():
    url = 'http://www.lzpcc.com.cn/xwzx/xxxw.htm'
    html = getHtml(url)
    getHtmlData(html)
    write(getHtmlData(html))


main()
